package com.realtyManage.springboot.Dao;

import com.realtyManage.springboot.Model.Do.Code;
import com.realtyManage.springboot.Model.Do.QuestionsAnswers;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import org.springframework.data.repository.query.Param;

public interface QuestionsAnswersRepository extends JpaRepository<QuestionsAnswers,Integer> {

    @Query(value = "SELECT * FROM questions_answers",nativeQuery = true)
    List<QuestionsAnswers> findQuestionsAnswers();

    @Query(value = "SELECT * FROM questions_answers where questions like %?1%",nativeQuery = true)
    List<QuestionsAnswers> findQuestionsAnswersE(String center);

    @Query(value = "SELECT * FROM questions_answers WHERE  is_show = 0 AND answers IS NULL ",nativeQuery = true)
    List<QuestionsAnswers> findQuestionsList();

    @Query(value = "SELECT * FROM questions_answers WHERE  is_show = 0 and id = ?1 ",nativeQuery = true)
    List<QuestionsAnswers> findQuestionsById(Integer id);

    @Modifying
    @Transactional
    @Query("update QuestionsAnswers a set " +
            "a.answers =  CASE WHEN :#{#questionsAnswers.answers} IS NULL THEN a.answers ELSE :#{#questionsAnswers.answers} END " +
            "where a.id = :#{#questionsAnswers.id}")
    int setAnswers(@Param("questionsAnswers") QuestionsAnswers questionsAnswers);

    @Query(value = "SELECT * FROM code1 ",nativeQuery = true)
    List<Code> findCode();

    @Modifying
    @Transactional
    @Query(value = "delete from questions_answers where id = ?1",nativeQuery = true)
    int deleteQuestions(Integer id);
}


